#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>

#define DEFAULT_PORT "27015"

int main(void) 
{
    struct addrinfo hints =
    {
        AI_PASSIVE,
        AF_INET,
        SOCK_STREAM,
        IPPROTO_TCP,
        0,
        NULL,
        NULL,
        NULL,
    };
    struct addrinfo *result = NULL;
    getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    int ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
    freeaddrinfo(result);
    listen(ListenSocket, SOMAXCONN);
    int ClientSocket = accept(ListenSocket, NULL, NULL);
    close(ListenSocket);
    char buf[32];
    int n = recv(ClientSocket, buf, sizeof buf, 0);
    printf("Bytes received: %d\n", n);
    n = send(ClientSocket, buf, n, 0 );
    printf("Bytes sent: %d\n", n);
    shutdown(ClientSocket, SHUT_WR);
    close(ClientSocket);

    return 0;
}